package action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import basic.PageInfo;
import basic.QuerySet;
import basic.ResultSet;
import basic.SortInfo;
import common.BaseAction;
import common.dict.CommonDict;
import entities.TMcc;
import interfaces.IMccService;
import net.sf.json.JSONArray;
import util.ParameterUtil;
import utils.DateUtil;
import utils.ExcelUtil;
import utils.StringUtil;

public class MccAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7507534255609348887L;

	private IMccService mccService;

	private TMcc mccBean;
	private List<TMcc> mccList;

	private String mccNo;
	private String mccNos;
	private String mccType;
	private String mccCode;
	private String mccName;
	private String mccPercent;
	private String mccState;
	private Date mccDate;
	private Date mccDateTop;
	private Date mccDateEnd;
	private String mccRemark;

	// 展示对象
	public String show() throws Exception {
		if (!mccService.exist(mccNo)) {
			addActionError("数据不存在");
			return INFO;
		} else {
			mccBean = mccService.fetch(mccNo);
			return SUCCESS;
		}
	}

	// 编辑对象
	public String edit() throws Exception {
		if (!mccService.exist(mccNo)) {
			mccBean = new TMcc();
		} else {
			mccBean = mccService.fetch(mccNo);
		}
		return SUCCESS;
	}

	// 持久化对象
	public String save() throws Exception {
		if (!mccService.exist(mccNo)) {
			mccBean = new TMcc();
		} else {
			mccBean = mccService.fetch(mccNo);
		}
		mccBean.setMccNo(mccNo);
		mccBean.setMccType(mccType);
		mccBean.setMccCode(mccCode);
		mccBean.setMccName(mccName);
		mccBean.setMccPercent(mccPercent);
		mccBean.setMccState(mccState);
		mccBean.setMccDate(mccDate);
		mccBean.setMccRemark(mccRemark);

		mccService.persist(mccBean);
		return SUCCESS;
	}

	// AUTOCOMPLATE功能
	public void auto() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("mccNo", StringUtil.split(mccNo));

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField("mccNo");
		sortInfo.setSortOrder(new Boolean(true));

		querySet.setPageInfo(new PageInfo(pageIndex, pageSize));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TMcc> resultSet = mccService.search(querySet);
		mccList = resultSet.getResultList();

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().println(JSONArray.fromObject(mccList).toString());
	}

	public String list() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("mccType", mccType);
		paramMap.put("mccCode", mccCode);
		paramMap.put("mccName", mccName);
		paramMap.put("mccState", mccState);
		paramMap.put("mccDate", ParameterUtil.dateCombin(mccDateTop, mccDateEnd));

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "mccDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(pageIndex, pageSize));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TMcc> resultSet = mccService.search(querySet);
		mccList = resultSet.getResultList();
		pageInfo = resultSet.getPageInfo();
		return SUCCESS;
	}

	// 导出数据 查询条件等同于列表查询条件
	public String export() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("mccType", mccType);
		paramMap.put("mccCode", mccCode);
		paramMap.put("mccName", mccName);
		paramMap.put("mccState", mccState);
		paramMap.put("mccDate", ParameterUtil.dateCombin(mccDateTop, mccDateEnd));

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "mccDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(1, CommonDict.EXPORT_LIMIT));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TMcc> resultSet = mccService.search(querySet);
		mccList = resultSet.getResultList();

		List<String> fieldHead = new ArrayList<String>();
		fieldHead.add("编号");
		fieldHead.add("mcc类别");
		fieldHead.add("mcc码");
		fieldHead.add("名称");
		fieldHead.add("基准费率");
		fieldHead.add("是否可用");
		fieldHead.add("日期日期");
		fieldHead.add("日期时间");
		fieldHead.add("备注");

		List<List<String>> fieldData = new ArrayList<List<String>>();

		for (TMcc mccBean : mccList) {
			List<String> fieldItem = new ArrayList<String>();
			fieldItem.add(mccBean.getMccNo());
			fieldItem.add(mccBean.getMccType());
			fieldItem.add(mccBean.getMccCode());
			fieldItem.add(mccBean.getMccName());
			fieldItem.add(mccBean.getMccPercent());
			fieldItem.add((mccBean.getMccState().equals("0")) ? "无效" : "有效");
			fieldItem.add(DateUtil.format(mccBean.getMccDate(), DateUtil.DATE));
			fieldItem.add(DateUtil.format(mccBean.getMccDate(), DateUtil.TIME));
			fieldItem.add(mccBean.getMccRemark());

			fieldData.add(fieldItem);
		}

		inputStream = ExcelUtil.excel(fieldHead, fieldData);
		return SUCCESS;
	}

	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////

	public IMccService getMccService() {
		return mccService;
	}

	public void setMccService(IMccService mccService) {
		this.mccService = mccService;
	}

	public TMcc getMccBean() {
		return mccBean;
	}

	public void setMccBean(TMcc mccBean) {
		this.mccBean = mccBean;
	}

	public List<TMcc> getMccList() {
		return mccList;
	}

	public void setMccList(List<TMcc> mccList) {
		this.mccList = mccList;
	}

	public String getMccNo() {
		return mccNo;
	}

	public void setMccNo(String mccNo) {
		this.mccNo = mccNo;
	}

	public String getMccNos() {
		return mccNos;
	}

	public void setMccNos(String mccNos) {
		this.mccNos = mccNos;
	}

	public String getMccType() {
		return mccType;
	}

	public void setMccType(String mccType) {
		this.mccType = mccType;
	}

	public String getMccCode() {
		return mccCode;
	}

	public void setMccCode(String mccCode) {
		this.mccCode = mccCode;
	}

	public String getMccName() {
		return mccName;
	}

	public void setMccName(String mccName) {
		this.mccName = mccName;
	}

	public String getMccPercent() {
		return mccPercent;
	}

	public void setMccPercent(String mccPercent) {
		this.mccPercent = mccPercent;
	}

	public String getMccState() {
		return mccState;
	}

	public void setMccState(String mccState) {
		this.mccState = mccState;
	}

	public Date getMccDate() {
		return mccDate;
	}

	public void setMccDate(Date mccDate) {
		this.mccDate = mccDate;
	}

	public Date getMccDateTop() {
		return mccDateTop;
	}

	public void setMccDateTop(Date mccDateTop) {
		this.mccDateTop = mccDateTop;
	}

	public Date getMccDateEnd() {
		return mccDateEnd;
	}

	public void setMccDateEnd(Date mccDateEnd) {
		this.mccDateEnd = mccDateEnd;
	}

	public String getMccRemark() {
		return mccRemark;
	}

	public void setMccRemark(String mccRemark) {
		this.mccRemark = mccRemark;
	}

}
